﻿using Heima.OA.Common;
using Heima.OA.IBLL;
using Heima.OA.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;

namespace Heima.OA.UI.Portal.Controllers
{
    public class HomeController : BaseController
    {
        public IUserInfoService UserInfoService { get; set; }
        public IActionInfoService ActionInfoService { get; set; }
        public string Index()
        {
            //if(Session["loginUser"] == null)
            //{
            //    RedirectToAction("Index", "UserLogin");

            //}
            Common.LogHelper.WriteLog("路由不匹配:Home/Index");
            return "路由不匹配";
        }

        //public ActionResult About()
        //{
        //    ViewBag.Message = "Your application description page.";

        //    return View();
        //}

        //public ActionResult Contact()
        //{
        //    ViewBag.Message = "Your contact page.";

        //    return View();
        //}

        public string LoadUserMenu()
        {
            int userId = this.LoginUser.Id;
            var user = UserInfoService.GetEntities(u => u.Id == userId).FirstOrDefault();
            //拿到当前用户的所有菜单权限
            var allRole = user.RoleInfo;
            var allRoleActionIds = (from r in allRole
                                 from a in r.ActionInfo
                                 select a.Id).ToList();
            var allDenyActionIDs = (from r in user.R_UserInfo_ActionInfo
                                   where r.HasPermission == (int)HasPermissionEnum.Refuse
                                   select r.ActionInfoId).ToList();

            //角色所有权限 剔除拒绝的权限
            var allActionIds = allRoleActionIds.Where(u => !allDenyActionIDs.Contains(u)).ToList();
            //用户特殊权限
            var allUserActionIds = (from t in user.R_UserInfo_ActionInfo
                                    where t.HasPermission == (int)HasPermissionEnum.Allow
                                    select t.ActionInfoId).ToList();
            //当前用户的所有权限 角色权限+特殊权限
            allActionIds.AddRange(allUserActionIds.AsEnumerable());
            //去重操作
            allActionIds = allActionIds.Distinct().ToList();

            var actionList = ActionInfoService.GetEntities(a => allActionIds.Contains(a.Id) && a.IsMenu == 1 && a.DelFlag == delflagNormal).ToList();
            //组装成页面要的 导航数据

            return JsonHandler.ToJson(actionList);
        }

    }
}